/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.vfi.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import edu.upn.sigemfi.ope.be.Ventanilla;
import edu.upn.sigemfi.vfi.bc.VentanillaDelegate;
import edu.upn.sigemfi.vfi.beans.VentanillaController;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Alex
 */
public class VentanillaConverter implements Converter{


        @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
      VentanillaDelegate delegado= new VentanillaDelegate();
        if (arg2 == null) {
            return null;
        }
        try {
            Long codigo = Long.parseLong(arg2);
            if (codigo != null) {
                Ventanilla det = delegado.buscar(codigo);
                return det;
            } else {
                return null;
            }
        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;

    }

  @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Ventanilla) {
            Ventanilla mo = (Ventanilla) arg2;
            return mo.getIdVentanilla().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo ventanilla");
        }
    }
    }